package com.itheima.stringdemo;

import java.util.Random;

/*
 *String案例2,随机产生验证码
 *
 * */
public class Test02 {
    public static void main(String[] args) {
        //独立方法,独立功能
        //目标:String开发验证码
        System.out.println(creat(5));
        System.out.println(creat(8));
    }

    private static String creat(int length) {
        String code = "";
        String data = "1qazxsw23edcvfr45tgbnhy67ujmki89olp0QAWSEZDXCVFRTGVBHYNJUIKMLOP";

        //1.直接使用循环控制随机获取多少位字符, 然后进行拼接返回即可
        //创建随机数对象
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            //2.随机随机产生索引值
            int index = random.nextInt(data.length());
            //按照索引从数据中随机抽取出来
            char a = data.charAt(index);
            //将取到的数据进行拼接
            code += a;
        }
        return code;
    }
}
